package com.exam.controller;

import com.exam.common.BaseInfoProperties;
import com.exam.entity.*;
import com.exam.service.AdminService;
import com.exam.serviceimpl.FillQuestionServiceImpl;
import com.exam.serviceimpl.JudgeQuestionServiceImpl;
import com.exam.serviceimpl.MultiQuestionServiceImpl;
import com.exam.serviceimpl.PaperServiceImpl;
import com.exam.util.ApiResultHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "试卷接口")
public class PaperController extends BaseInfoProperties {

    @Autowired
    private PaperServiceImpl paperService;

    @Autowired
    private JudgeQuestionServiceImpl judgeQuestionService;

    @Autowired
    private MultiQuestionServiceImpl multiQuestionService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private FillQuestionServiceImpl fillQuestionService;
    @GetMapping("/papers")
    public ApiResult<PaperManage> findAll() {
       ApiResult res =  ApiResultHandler.buildApiResult(200,"请求成功",paperService.findAll());
       return  res;
    }
//查询是否在考试时间内,是的话，返回考试试卷
    @GetMapping("/paper/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId) {
        paperService.findExamTimeByPaperId(paperId); //查询考试时间
        Map<Integer, List<?>> map = findPaper(paperId);
        return  map;
    }
    //管理员预览试卷or学生查看试卷
    @GetMapping("/paper")
    public Map<Integer, List<?>> findByIdPreview(@RequestParam String paperId, @RequestParam(defaultValue = "") String adminId) {
        Map<Integer, List<?>> map=null;
        //老师预览试卷
        if(!adminId.equals("")){
            //判断是否是管理员
            int admin = adminService.isAdmin(Integer.parseInt(adminId));
            if(admin==1){
                //是管理员
                map = findPaper(Integer.parseInt(paperId));
            }else{
                return null;
            }
        }else {
            //学生查看试卷
            map = findPaper(Integer.parseInt(paperId));
        }
        return  map;
    }

 /*   @GetMapping("/papers/{examCode}")
    public Map<Integer, List<?>> findPaperByExamCode(@PathVariable("examCode") Integer examCode) {
        int paperId = paperService.findPaperIdByExamCode(examCode);
        Map<Integer, List<?>> map = findPaper(paperId);
        return  map;
    }*/

    @PostMapping("/paperManage")
    public ApiResult add(@RequestBody PaperManage paperManage) {
        int res = paperService.add(paperManage);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

    //查询考生是否已经考过试，如果考过，则不能再考试
    @GetMapping("/paper/{examCode}/{studentId}")
    @ApiOperation(value = "查询考生是否已经考过试，如果考过，则不能再考试",notes = "查询考生是否已经考过试，如果考过，则不能再考试")
    public ApiResult findPaperByExamCode(@PathVariable("examCode") Integer examCode,@PathVariable("studentId") Integer studentId) {
        paperService.findPaperRow(examCode,studentId);
        return ApiResultHandler.buildApiResult(200,"查询成功！",examCode);
    }
}
